
$(function() {
	
	$("input[id^='emp']:visible").autocomplete(Test.emp);
	
	_change("input[id='bank']:visible", "select[id='bank_t']:visible");
	
	$("input[id^='bankcode']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=BankCode.ajax_zjm',
				dataType: "json",
				data: {
					bank: $('#bank').val(),
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {						
						return {
							branch: item.branch,
							label: item.code + " [" + item.branch + "]",
							value: item.code + ":" + item.branch
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='branch']:visible").val(ui.item.branch);
		},
		
		minLength: 1
	});
	
	$("input[id^='province']:visible").autocomplete({
		minLength: 0,
		source: function( request, response ) {
			var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
			
			response( $.map( provinces, function( item ) {
				if ( item && ( !request.term || matcher.test(item.zjm) ) )
					return {
					label: item.name + " [" + item.zjm + "]",
					value: item.name
				};
			}) );
		}
	});	

	$("input[id^='city']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=Zone.ajax_zjm2',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							zone: item.zone,
							label: item.zjm + " [" + item.city + ", " + item.zone + "]",
							value: item.city
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='zone']:visible").val(ui.item.zone);
		},
		
		minLength: 1
	});
	
	$("input[id^='zone']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'info.php?do=Zone.ajax_zjm2',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							city: item.city,
							label: item.zjm + " [" + item.city + ", " + item.zone + "]",
							value: item.zone
						}
					}));
				}
			});
		},
		
		select: function( event, ui ) {
			$("input[id^='city']:visible").val(ui.item.city);
		},
		
		minLength: 1
	});
	
	$.extend({
		format: function(str, step, splitor) {
			str = str.toString();
			var len = str.length;
 
			if (len > step) {
				var i = 0, s = "", arr = [];
				while (i < len) {
					s = str.substr(i, step);					
					arr.push(s);					
					i += step;					
				}				 
				 
				str = arr.join(splitor);
			 }
			
			 return str;
		}
	});
	
	var code = $("input[id^='code']:visible").val();
	$("input[id^='code']:visible").val($.format(code.replace(/\s/g, ''), 4, ' '));
	
	$("input[id^='code']:visible").keyup(function() {
		var s = $(this).val();
		// alert(s);
		var a = s.replace(/\s/g, '');
		
		var b = $.format(a, 4, ' ');
		$(this).val(b);
	});
});
